#!/usr/bin/env python
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



# wildproxy allows you to run a program that doesn't accept multiple input files on multiple files.  You supply a commandline as the first argument, and a list of input files as the remaining arguments, and wildproxy executes the commandline for you.
#
# The commandline should be enclosed in single quotes: ''.  It understands the following replacements:
# !f -- the filename of the file being operated on
# !b -- the basename (the part before the extension) of the file being operated on
# !e -- the extension of the file being operated on (with the dot, if an extension is present)
# The choice of ! will cause problems if your command or any of the files have a ! in them, but this doesn't seem very likely.

import sys
import os

if len(sys.argv) < 3:
    print("\nwildproxy: not enough arguments.\n"
        "usage: wildproxy 'command to execute' filenames\n"
        "!f   filename\n"
        "!b   basename\n"
        "!e   extension")
    sys.exit()

command = sys.argv[1]
filenames = sys.argv[2:]
for filename in filenames:
    [basename,extension] = os.path.splitext(filename)
    # Perform replacements and execute
    cmdstr = command.replace('!f', '"%s"' % (filename))
    cmdstr = cmdstr.replace('!b', '"%s"' % (basename))
    cmdstr = cmdstr.replace('!e', '"%s"' % (extension))
    print(cmdstr)
    os.system(cmdstr)
